package LatentTicTacToe.Game;

import java.util.Scanner;

import LatentTicTacToe.Data.Board;
import LatentTicTacToe.Data.Move;
import LatentTicTacToe.Players.CFRPlayer;
import LatentTicTacToe.Players.HumanPlayer;
import LatentTicTacToe.Players.MCTSPlayer;
import LatentTicTacToe.Players.OverconfidentPlayer;
import LatentTicTacToe.Players.ParanoidPlayer;
import LatentTicTacToe.Players.Player;

public class GameRunner {
	
	private static final int WIDTH = 3;
	private static final int HEIGTH = 3;
	
	private Board board;
	
	private static Player crossPlayer = null;
	private static Player circlePlayer = null;
	private static Player currentPlayer = null;

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		String playerOne;
		String playerTwo;
		

		System.out.println("******************************************");
		System.out.println("Welcome in game Latent Tic-tac-toe");
		System.out.println("******************************************");

		do {
			System.out.println("Please choose player one from following list:");
			System.out.println("Type 1 for MCTS player.");
			System.out.println("Type 2 for CFR player.");
			System.out.println("Type 3 for Paranoid player.");
			System.out.println("Type 4 for Overconfident player.");
			System.out.println("Type 5 for Human player.");

			playerOne = scanner.next();

			if (playerOne.equals("1")) {
				crossPlayer = new MCTSPlayer(new Board(WIDTH, HEIGTH, (byte)1), (byte)1);
			} else if (playerOne.equals("2")) {
				crossPlayer = new CFRPlayer(new Board(WIDTH, HEIGTH, (byte)1), (byte)1);
			} else if (playerOne.equals("3")) {
				crossPlayer = new ParanoidPlayer(new Board(WIDTH, HEIGTH, (byte)1), (byte)1);
			} else if (playerOne.equals("4")) {
				crossPlayer = new OverconfidentPlayer(new Board(WIDTH, HEIGTH, (byte)1), (byte)1);
			} else if (playerOne.equals("5")) {
				crossPlayer = new HumanPlayer(new Board(WIDTH, HEIGTH, (byte)1), (byte)1);
			}
		} while (crossPlayer == null);
		
		do {
			System.out.println("Please choose player two from following list:");
			System.out.println("Type 1 for MCTS player.");
			System.out.println("Type 2 for CFR player.");
			System.out.println("Type 3 for Paranoid player.");
			System.out.println("Type 4 for Overconfident player.");
			System.out.println("Type 5 for Human player.");

			playerTwo = scanner.next();

			if (playerTwo.equals("1")) {
				circlePlayer = new MCTSPlayer(new Board(WIDTH, HEIGTH, (byte)1), (byte)2);
			} else if (playerTwo.equals("2")) {
				circlePlayer = new CFRPlayer(new Board(WIDTH, HEIGTH, (byte)1), (byte)2);
			} else if (playerTwo.equals("3")) {
				circlePlayer = new ParanoidPlayer(new Board(WIDTH, HEIGTH, (byte)1), (byte)2);
			} else if (playerTwo.equals("4")) {
				circlePlayer = new OverconfidentPlayer(new Board(WIDTH, HEIGTH, (byte)1), (byte)2);
			} else if (playerTwo.equals("5")) {
				circlePlayer = new HumanPlayer(new Board(WIDTH, HEIGTH, (byte)1), (byte)2);
			}
		} while (circlePlayer == null);
		
		new GameRunner().runGame();

	}
	
	private void runGame() {

		Move move;
		Board tempBoard;

		boolean valid = true;

		board = new Board(WIDTH, HEIGTH, (byte)1);

		currentPlayer = crossPlayer;

		while (board.isTerminal() == 0) {

			do {
				System.out.println("id: " + board.id);

				System.out.println("Latent situation");
				for (int i = 0; i < board.latentSituation.length; i++) {
					for (int j = 0; j < board.latentSituation[0].length; j++) {
						System.out.print(board.latentSituation[j][i]);
					}
					System.out.println();
				}
				System.out.println("Real situation");
				for (int i = 0; i < board.latentSituation.length; i++) {
					for (int j = 0; j < board.latentSituation[0].length; j++) {
						System.out.print(board.realSituation[j][i]);
					}
					System.out.println();
				}
				System.out.println("Available moves: " + board.getSuccessors());

				tempBoard = new Board(board, board.id, currentPlayer.sign);
				move = currentPlayer.getNextNode(tempBoard, valid);
				valid = board.isValid(move);

				board.makeMove(move, currentPlayer.sign);

			} while (!valid);

			switchPlayers();
		}

		System.out.println("Latent situation");
		for (int i = 0; i < board.latentSituation.length; i++) {
			for (int j = 0; j < board.latentSituation[0].length; j++) {
				System.out.print(board.latentSituation[j][i]);
			}
			System.out.println();
		}
		System.out.println("Real situation");
		for (int i = 0; i < board.latentSituation.length; i++) {
			for (int j = 0; j < board.latentSituation[0].length; j++) {
				System.out.print(board.realSituation[j][i]);
			}
			System.out.println();
		}

		 System.out.println();
		 System.out.println("*************************");
		if (board.isTerminal() == -1) {
			 System.out.println("It's a tie.");
		} else {
			System.out.println("Player " + board.isTerminal() + " won.");
		}
	}

	private void switchPlayers() {
		if (currentPlayer.sign == 1) {
			currentPlayer = circlePlayer;
		} else {
			currentPlayer = crossPlayer;
		}
	}
}
